package spring.chatroom.java_chatroom.controller;

import io.jsonwebtoken.lang.Strings;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import spring.chatroom.java_chatroom.common.constant.Constants;
import spring.chatroom.java_chatroom.common.utils.Base64Util;
import spring.chatroom.java_chatroom.common.utils.RedisUtil;
import spring.chatroom.java_chatroom.common.utils.RegexUtil;
import spring.chatroom.java_chatroom.common.utils.SecurityUtil;
import spring.chatroom.java_chatroom.controller.pojo.*;
import spring.chatroom.java_chatroom.mapper.UserMapper;
import spring.chatroom.java_chatroom.pojo.model.UserInfo;
import spring.chatroom.java_chatroom.pojo.response.Response;
import spring.chatroom.java_chatroom.server.SettingServer;

import java.io.IOException;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("set")
public class SettingController {

    @Autowired
    SettingServer settingServer;

    @Autowired
    RedisUtil redisUtil;

    @Autowired
    UserMapper userMapper;

    @Autowired
    Constants constants;

    @RequestMapping("set-user-info")
    public Response setInfo(@NotNull @RequestBody SettingUserInfo userInfo) {
        log.info("收到更改设置的请求 {}, {}", userInfo.getUserId(), userInfo.getUserName());

        if (!Strings.hasLength(userInfo.getUserName())) {
            userInfo.setUserName(null);
        }
        if (!Strings.hasLength(userInfo.getSign())) {
            userInfo.setSign(null);
        }
        if (!Strings.hasLength(userInfo.getHeadPortrait()) || userInfo.getHeadPortrait().equals("#")) {
            userInfo.setHeadPortrait(null);
        }

        settingServer.saveModify(userInfo);

        redisUtil.remove(Constants.USERINFO + userInfo.getUserId());

        return Response.success("true");
    }

    @RequestMapping("set-password")
    public Response setPassword(@RequestBody @NotNull SetPassword request) {
        log.info("收到修改密码的请求 {}", request);

        //旧密码是否正确
        UserInfo userByUserId = userMapper.getUserByUserId(request.getUserId());

        if (!SecurityUtil.verify(request.getOldPassword(), userByUserId.getPassword())) {
            return Response.success(Map.of("status", "fail", "msg", "旧密码错误"));
        } else if (SecurityUtil.verify(request.getNewPassword(), userByUserId.getPassword())) {
            return Response.success(Map.of("status", "fail", "msg", "新密码不能和旧密码相同"));
        }

        //修改密码
        request.setNewPassword(SecurityUtil.encrypt(request.getNewPassword()));

        userMapper.modifyPassword(request);

        return Response.success(true);
    }

    @RequestMapping("set-email")
    public Response setEmail(@RequestBody @NotNull SetEmail request) {
        log.info("收到修改邮箱请求 {}", request);

        //检测邮箱有没有呗使用
        if (userMapper.getUserInfoByEmail(request.getEmail()) != null) {
            return Response.success(Map.of("status", "fail", "msg","邮箱已经被注册"));
        }

        if (request.getOldEmail() == null) {
            //绑定邮箱
            userMapper.modifyEmail(request);
            return Response.success(Map.of("status", "success"));
        }

        if (!RegexUtil.checkMail(request.getOldEmail().equals("null") ? request.getEmail() : request.getOldEmail()) ) {
            return Response.success(Map.of("status", "fail", "msg", "邮箱格式错误"));
        }

        userMapper.modifyEmail(request);

        return Response.success(Map.of("status", "success"));
    }

//    backgroundUrl: 模糊度-url
    @RequestMapping("set-background")
    public Response setBackground(@RequestBody @NotNull SetBackground request) throws IOException {
        log.info("收到修改网背景页请求 userId: {}", request.getUserId());

        String s = request.getBackgroundUrl();

        //这里判断是不是有新的文件
        if(request.getImageUrlBase64() != null) {
            s = Base64Util.saveBase64ToFile(request.getImageUrlBase64(), constants.localBackgroundPath);
            request.setBackgroundUrl(s);
        }

        request.setBackgroundUrl(request.getBlur() + "-" + s);

        userMapper.modifyBackGround(request);

        redisUtil.remove(Constants.USERINFO + request.getUserId());

        return Response.success(Map.of("status", "success", "imgUrl", s.replace("/chat-background/", "")));
    }

    @RequestMapping("set-chatBackground")
    public Response setChatBackground(@RequestBody @NotNull SetChatBackground request) {
        log.info("收到修改聊天背景请求 {}", request);

        return Response.success(Map.of("status", "success"));
    }

}
